/**
 * 内存系统统一导出 - IntelliMark版
 */

// 导出所有内存实现
export * from './conversationalMemory'
export * from './vectorStoreMemory'
export * from './episodicMemory'

// 导出内存类型
export type {
  BaseMemory,
  MemoryType,
  ConversationalMemory,
  VectorStoreMemory,
  EpisodicMemory,
  Episode,
  EpisodeFilter,
  EpisodeMetadata,
  MemorySearchParams,
  MemorySearchResult,
  MemoryConfig,
} from '../types/memory'

// 导出便捷创建函数
export { default as ConversationalMemory } from './conversationalMemory'
export { default as VectorStoreMemory } from './vectorStoreMemory'
export { default as EpisodicMemory } from './episodicMemory'

// 内存管理器
export class MemoryManager {
  private static instance: MemoryManager
  private memorySystems: Map<string, any> = new Map()

  private constructor() {
    this.initializeDefaultMemories()
  }

  public static getInstance(): MemoryManager {
    if (!MemoryManager.instance) {
      MemoryManager.instance = new MemoryManager()
    }
    return MemoryManager.instance
  }

  private initializeDefaultMemories(): void {
    // 注册默认内存系统
    this.registerMemory('conversation', new ConversationalMemory('default'))
    this.registerMemory('vector', new VectorStoreMemory('default'))
    this.registerMemory('episodic', new EpisodicMemory('default'))
  }

  public registerMemory(name: string, memory: any): void {
    this.memorySystems.set(name, memory)
  }

  public getMemory(name: string): any {
    return this.memorySystems.get(name)
  }

  public getAllMemories(): Map<string, any> {
    return new Map(this.memorySystems)
  }

  public listMemories(): string[] {
    return Array.from(this.memorySystems.keys())
  }

  // 便捷方法
  public async storeConversation(messages: any[]): Promise<void> {
    const memory = this.getMemory('conversation')
    if (memory) {
      for (const message of messages) {
        await memory.addMessage(message)
      }
    }
  }

  public async storeDocument(content: string, metadata?: any): Promise<void> {
    const memory = this.getMemory('vector')
    if (memory) {
      await memory.addDocument({
        pageContent: content,
        metadata: metadata || {},
      })
    }
  }

  public async storeEpisode(episode: any): Promise<void> {
    const memory = this.getMemory('episodic')
    if (memory) {
      await memory.addEpisode(episode)
    }
  }

  public async searchMemory(query: string, type?: string): Promise<any[]> {
    if (type) {
      const memory = this.getMemory(type)
      if (memory && memory.search) {
        return await memory.search(query)
      }
    }

    // 搜索所有内存系统
    const results: any[] = []
    for (const [name, memory] of this.memorySystems) {
      if (memory.search) {
        try {
          const memoryResults = await memory.search(query)
          results.push(...memoryResults.map((r: any) => ({ ...r, memoryType: name })))
        } catch (error) {
          console.warn(`Search failed for memory ${name}:`, error)
        }
      }
    }
    return results
  }

  public getStats(): any {
    const stats: any = {}
    this.memorySystems.forEach((memory, name) => {
      stats[name] = {
        type: memory.type,
        enabled: memory.enabled,
        size: memory.messages?.length || memory.documents?.length || memory.episodes?.length || 0,
      }
    })
    return stats
  }

  public async clearAll(): Promise<void> {
    for (const memory of this.memorySystems.values()) {
      if (memory.clear) {
        await memory.clear()
      }
    }
  }
}

// 导出便捷函数
export const getMemoryManager = (): MemoryManager => {
  return MemoryManager.getInstance()
}